package com.ssm.modules.controller;

import com.ssm.common.bean.VResponse;
import com.ssm.modules.entity.Course;
import com.ssm.modules.entity.Grade;
import com.ssm.modules.entity.User;
import com.ssm.modules.service.ICourseService;
import com.ssm.modules.service.IGradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @email:
 * @author:
 * @date: 2018/12/18
 * @time: 17:22
 * 课程Controller
 */
@Controller
@RequestMapping("course")
public class CourseController {
    @Autowired
    private ICourseService courseService;
    @Autowired
    private IGradeService gradeService;
    /**
     * 获取课程
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectCourse", method = RequestMethod.GET)
    public VResponse<List<Course>> selectCourse() {
        List<Course> courses = courseService.selectAll();
        return VResponse.success(courses);
    }

    /**
     * 增加课程
     * @param course
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addCourse", method = RequestMethod.POST)
    public VResponse<Object> addCourseSubmit(@RequestBody Course course) {
        courseService.addCourse(course);
        return VResponse.success("添加成功");

    }

    /**
     * 修改课程
     * @param course
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateCourse", method = RequestMethod.PUT)
    public VResponse<Object> updateCourse(@RequestBody Course course) {
        this.courseService.updateCourse(course);
        return VResponse.success("修改成功");
    }

    /**
     * 删除课程
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteCourse",method = RequestMethod.DELETE)
    public VResponse<Object> deleteCourse(@RequestBody Course course) {
        this.courseService.deleteCourse(course.getId());
        return VResponse.success("删除成功！");
    }

    @RequestMapping(value = "stuCourseScore", method = RequestMethod.GET)
    public String stuCourseScore(@RequestParam(value = "page" ,defaultValue = "-1")int page,
                                 @RequestParam(value = "pageSize" ,defaultValue = "15")int pageSize,
                                 @RequestParam("stuId") int stuId,
                                 Map<String, Object> map) {

        List<Course> courses = courseService.findByStuId(stuId, page, pageSize);
        int amount = courseService.findByStuIdCount(stuId);

        map.put("page", page);
        map.put("courses", courses);
        map.put("pageItemSize", (int)Math.ceil(amount / (double)pageSize));
        return "/stu/course";
    }

    @RequestMapping(value = "courseManagement", method = RequestMethod.GET)
    public String courseManagement(@RequestParam(value = "page" ,defaultValue = "-1")int page,
                                   @RequestParam(value = "pageSize" ,defaultValue = "15")int pageSize,
                                   Map<String, Object> map,
                                   HttpServletRequest request) {
        try {
            User user = (User) request.getSession().getAttribute("user");
            List<Course> courses = courseService.findByTeaId(user.getId(), page, pageSize);
            int amount = courseService.findByTeaIdCount(user.getId());

            List<Grade> grades = gradeService.selectAll();
            map.put("grades", grades);

            map.put("page", page);
            map.put("courses", courses);
            map.put("pageItemSize", (int)Math.ceil(amount / (double)pageSize));
            return "/tea/courseManagement";
        }catch (Exception e) {

            return "/error/404";
        }
    }

    @RequestMapping(value = "courseGrade", method = RequestMethod.GET)
    public String courseGrade(@RequestParam(value = "courseId" , defaultValue = "0")int courseId,
                              @RequestParam(value = "page" , defaultValue = "-1")int page,
                              @RequestParam(value = "pageSize" , defaultValue = "15")int pageSize,
                              Map<String, Object> map,
                              HttpServletRequest request) {
        try {
            User user = (User) request.getSession().getAttribute("user");
            //  查询该教师的所有课程
            List<Course> myCourses = courseService.findByTeaId(user.getId(), -1, pageSize);
            //  获取课程和对应学生信息
            List<Course> courses = courseService.findByTeaIdAndCourseId(user.getId(), courseId, page, pageSize);
            int amount = courseService.findByTeaIdAndCourseIdCount(user.getId(), courseId);

            List<Grade> grades = gradeService.selectAll();
            map.put("courseId", courseId);
            map.put("grades", grades);
            map.put("myCourses", myCourses);
            map.put("page", page);
            map.put("courses", courses);
            map.put("pageItemSize", (int)Math.ceil(amount / (double)pageSize));//记录存入的总页数
            return "/tea/courseGrade";
        }catch (Exception e){

            return "/error/404";
        }
    }

    @ResponseBody
    @RequestMapping(value = "courseGrade", method = RequestMethod.PUT)
    public VResponse<Object> courseGrade(@RequestBody Course course) {
        int grade = courseService.grade(course.getStu().getId(), course.getScore(), course.getId());
        if(grade > 0){
            return VResponse.success("评分成功");
        }
        return VResponse.success("评分成功");
    }


}

